package com.example.weather.More

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.example.weather.R
import com.example.weather.data.Person

/**
 * @author Y7993
 */
//显示姓名电话的适配器
// 联系人适配器，继承自BaseAdapter，用于显示联系人信息
class ContactAdapter(var context: Context?, var personList: MutableList<Person?>) : BaseAdapter() {

    // 返回联系人总数
    override fun getCount(): Int {
        return personList.size
    }

    // 根据索引返回对应的联系人对象
    override fun getItem(i: Int): Any? {
        return personList[i]
    }

    // 返回联系人的索引值
override fun getItemId(i: Int): Long {
    return i.toLong()
}


    @SuppressLint("InflateParams", "ViewHolder")  // 根据位置返回视图，显示联系人姓名和电话
    override fun getView(i: Int, view: View?, viewGroup: ViewGroup?): View {
        var holder: ViewHolder?
        var convertView: View = LayoutInflater.from(context).inflate(R.layout.item_phone, null)
        holder = ViewHolder(convertView)
        convertView.tag = holder
        // 设置姓名和电话号码到视图中
        holder.nv.text = personList[i]!!.name
        holder.pv.text = personList[i]!!.number

        return convertView
    }

    // 视图持有者类，用于缓存视图组件
    internal class ViewHolder(itemView: View) {
        var pv: TextView = itemView.findViewById<TextView>(R.id.item_phone_number)
        var nv: TextView = itemView.findViewById<TextView>(R.id.item_phone_name)
    }
}
